package com.yunnuo.system.menu.controller;


import com.yunnuo.system.comm.util.ResultData;
import com.yunnuo.system.menu.entity.Menu;
import com.yunnuo.system.menu.service.IMenuService;
import com.yunnuo.system.role.entity.Role;
import com.yunnuo.system.role.service.IRoleMenuService;
import net.sf.json.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @api {title} 菜单信息comtroller
 * @apiName MenuController
 * @apiGroup System-MenuController
 * @apiDescription Created by 魏洪福(15064000912@163.com) on  2018/5/25.
 */
@Controller
@RequestMapping("/system/menu")
public class MenuController {

    private String prefix = "system/menu";

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IRoleMenuService roleMenuService;

    @RequiresPermissions("system:menu:view")
    @GetMapping()
    public String menu()
    {
        return prefix + "/menu";
    }

    @RequiresPermissions("system:menu:list")
    @GetMapping("/list")
    @ResponseBody
    public List<Menu> list()
    {
        List<Menu> menuList = menuService.findList();
        return menuList;
    }

    /**
     * 删除菜单
     */
    @RequiresPermissions("system:menu:remove")
    @GetMapping("/remove/{menuId}")
    @ResponseBody
    public JSON remove(@PathVariable("menuId") String menuId)
    {
        ResultData data = new ResultData();
        if (menuService.selectCountMenuByParentId(menuId) > 0)
        {
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("在子菜单,不允许删除！");
        }
        if (roleMenuService.selectCountRoleMenuByMenuId(menuId) > 0)
        {
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("菜单已分配,不允许删除！");
        }
            menuService.deleteVo(menuId);
            data.setStat(ResultData.STAT_SUCCESS);
            data.setMessage("操作成功！");
        return data.toJson();
    }

    /**
     * 修改菜单
     */
    @RequiresPermissions("system:menu:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, Model model)
    {
        Menu menu = menuService.selectMenuById(id);
        model.addAttribute("menu", menu);
        return prefix + "/edit";
    }

    /**
     * 新增
     */
    @RequiresPermissions("system:menu:add")
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") String parentId, Model model)
    {
        Menu menu = null;
        if (!parentId.equals("0"))
        {
            menu = menuService.findVO(parentId);
        }
        else
        {
            menu = new Menu();
            menu.setIcon(0L+"");
            menu.setMenuName("主目录");
        }
        model.addAttribute("menu", menu);
        return prefix + "/add";
    }

    /**
     * 保存菜单
     */
    @RequiresPermissions("system:menu:save")
    @PostMapping("/save")
    @ResponseBody
    public JSON save(Menu menu)
    {
        ResultData data = new ResultData();
        try{
            menuService.addVO(menu);
            data.setStat(ResultData.STAT_SUCCESS);
            data.setMessage("操作成功！");
        }catch (Exception e){
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("操作失败！");
        }
        return data.toJson();
    }

    /**
     * 选择菜单图标
     */
    @GetMapping("/icon")
    public String icon()
    {
        return prefix + "/icon";
    }

    /**
     * 校验菜单名称
     */
    @PostMapping("/checkMenuNameUnique")
    @ResponseBody
    public String checkMenuNameUnique(Menu menu)
    {
        String uniqueFlag = "0";
        if (menu != null)
        {
//            uniqueFlag = menuService.checkMenuNameUnique(menu);
        }
        return uniqueFlag;
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Map<String, Object>> roleMenuTreeData(Role role)
    {
        List<Map<String, Object>> tree = menuService.roleMenuTreeData(role);
        return tree;
    }
    
    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Map<String, Object>> menuTreeData(Role role)
    {
        List<Map<String, Object>> tree = menuService.menuTreeData();
        return tree;
    }
    
    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") String  menuId, Model model)
    {
        model.addAttribute("treeName", menuService.findVO(menuId).getMenuName());
        return prefix + "/tree";
    }
}